package com.geezdata.cps.sdk3rd.miya.response;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.sdk3rd.miya.domain.MiyaOrder;

public class MiyaSearchResult extends MiyaResult{
    private Integer total;
    private List<MiyaOrder> orders;
    private String requestId;
    
    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public List<MiyaOrder> getOrders() {
        return orders;
    }

    public void setOrders(List<MiyaOrder> orders) {
        this.orders = orders;
    }

    public String getRequestId() {
        return requestId;
    }
    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

	@Override
	public void parseResponseJson(JSONObject responseJson) throws MyException {
        JSONObject content = responseJson.getJSONObject("content");
        JSONObject ordersListResponse = content.getJSONObject("orders_list_response");
        total = ordersListResponse.getInteger("total");
        if (total != null && total != 0) {
            JSONArray ordersJsons = ordersListResponse.getJSONArray("order_list");
            this.orders = new ArrayList<MiyaOrder>();
            for (int i = 0; i < ordersJsons.size(); i++) {
                JSONObject orderJson = ordersJsons.getJSONObject(i);
                if (orderJson == null) {
                    continue;
                }

                this.orders.add(MiyaOrder.convert(orderJson));
            }
        }
        
        this.requestId = responseJson.getString("request_id");
	}
}
